﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class CircleMemberManage : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            CircleMemberManageOutParam result = new CircleMemberManageOutParam();
            CircleMemberManageInParam param = new CircleMemberManageInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            { 
                param = inParam as CircleMemberManageInParam;
                if (string.IsNullOrEmpty(param.user_id) || string.IsNullOrEmpty(param.community_id)
                    || string.IsNullOrEmpty(param.opertype) || string.IsNullOrEmpty(param.opuser_id))
                {
                    result.code = "0";
                    result.desc = "用户ID,群ID,操作人ID,操作类别不能为空";

                    isFlag = false;
                }

                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }

                if(isFlag)
                {
                    if (param.opertype != "1" && param.opertype != "2" && param.opertype != "3")
                    {
                        result.code = "0";
                        result.desc = "操作类别只能为1(删除)或2(审核通过)或3(审核不通过)"; 

                        isFlag = false;
                    }
                }

                if (isFlag && param.opertype == "2")
                {
                    DataSet ds = InterfaceHelper.GetCircleMemberNumInfo(param.community_id);

                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "该群不存在";

                        isFlag = false;
                    }
                    else if (Convert.ToInt32(ds.Tables[0].Rows[0]["MaxMemberNum"]) <= Convert.ToInt32(ds.Tables[0].Rows[0]["CurrentMemberNum"]))
                    {
                        result.code = "0";
                        result.desc = "该群已经满员";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    //保存数据
                    InterfaceHelper.CircleMemberManage(param.community_id, param.user_id, param.opuser_id, param.opertype);
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-CircleMemberManage", SerializeHelper.JsonSerializer<CircleMemberManageInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<CircleMemberManageInParam, CircleMemberManageOutParam>();
            else
                SafeProcessRequest<CircleMemberManageInParam, CircleMemberManageOutParam>();
        }
    }

    #region 请求参数
    public class CircleMemberManageInParam
    {
        /// <summary>
        /// 群ID 必须
        /// </summary>
        public string community_id
        { get; set; }  

        /// <summary>
        ///	成员ID 必须
        /// </summary>
        public string user_id
        { get; set; }  
    
        /// <summary>
        ///	操作人ID 必须
        /// </summary>
        public string opuser_id
        { get; set; }

        /// <summary>
        ///	操作类别 必须，1：删除，2：审核通过，3：审核不通过
        /// </summary>
        public string opertype
        { get; set; }     
    }
    #endregion

    #region 结果参数
    public class CircleMemberManageOutParam
    {
        public CircleMemberManageOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    }  
    #endregion 
}